// pages/im/complaintPage/complaintPage.js
import { cooperationBehavior } from "../../../behaviors/cooperation.js";
import api from "../../../common/api.js";
import p from "../../../utils/platform.js";
import util from "../../../utils/util.js";

const app = getApp();

Page({
  behaviors: [cooperationBehavior],

  /**
   * 页面的初始数据
   */
  data: {
    list: [
      { code: 1, label: "房源不可合作" },
      { code: 2, label: "恶意骗取房源信息" },
      { code: 3, label: "其他" },
    ],
    code: 1,
    value: "",
    imgMax: 3, //最大图片数
    maxCount: 200,
    imgArray: [],
    delId: "",
    beComplaintedUser: "",
    cooperationId: "",
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let cooperationId = options.cooperationId;
    let unionId = options.beComplaintedUser;
    
    this.setData({
      cooperationId,
    });

    api.getJJRVO({ unionId }, ({ data }) => {
      let id = data.content.id
      this.getInfoByCooperationId(cooperationId).then((content) => {
        this.setData({
          beComplaintedUser: id,
          delId: content.hmCooperationDelVO.delId,
        });
      });
    });
  },

  radioChange(e) {
    this.setData({
      code: e.detail.value,
    });
  },

  handleInput(e) {
    const value = e.detail.value;
    this.setData({
      value: value.slice(0, this.data.maxCount),
    });
  },

  handleSelectImg() {
    let num = this.data.imgMax - this.data.imgArray.length;
    if (num == 0) return wx.showToast({ icon: "none", title: "已达最大图片数！" });

    wx.chooseImage({
      sourceType: ["album"], // 从相册选择
      count: num,
      success: (res) => {
        let filePathArr = res.tempFilePaths;
        filePathArr.forEach((item) => {
          util.uploadFiles(item, (res) => {
            console.log("上传图片成功====", res);
            try {
              var id = JSON.parse(res.data)[0].id;
              var obj = {
                fileId: id,
                url: item,
              };
  
              let arr = this.data.imgArray.concat(obj);
              if (arr.length > this.data.imgMax) {
                arr.length = this.data.imgMax;
              }
              this.setData({
                imgArray: arr,
              });
              
            } catch (error) {
              
            }
          });
        });
      },
    });
  },

  handleCancel(e) {
    let id = e.currentTarget.dataset.fileid;

    this.setData({
      imgArray: this.data.imgArray.filter((v) => v.fileId != id),
    });
  },

  handleConfirm() {
    let label = this.data.list.find((v) => v.code == this.data.code).label;

    api.complaintAdd(
      {
        beComplaintedUser: this.data.beComplaintedUser,
        delId: this.data.delId,
        complaintReason: label,
        complaintProof: this.data.imgArray.map((v) => v.fileId),
        complaintRemark: this.data.value,
      },
      ({ data }) => {
        if (data.isSuccess) {
          app && app.eventBus && app.eventBus.trigger("complain_jjr");

          p.showModal({
            content: `投诉成功，返回聊天页面！`,
            showCancel: false
          }).then(({confirm}) => {
            if (confirm) {
              p.navigateBack()
            }
          })
        }
      }
    );
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {},

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {},

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {},

});
